package wechat.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import wechat.resources.InstanceResource;
import wechat.service.AuthService;
import wechat.service.base.BaseRequest;
import wechat.util.WechatUtil;

@Service
public class IAuthService extends BaseRequest implements AuthService {
//	wx.config({"debug":false,"appId":"wx2958809f12298333","timestamp":1456217267,"nonceStr":"3rGoIwTM5JBk94yP","jsApiList":["checkJsApi","onMenuShareTimeline","onMenuShareAppMessage","onMenuShareQQ","onMenuShareWeibo","hideMenuItems","showMenuItems","hideAllNonBaseMenuItem","showAllNonBaseMenuItem","translateVoice","startRecord","stopRecord","onRecordEnd","playVoice","pauseVoice","stopVoice","uploadVoice","downloadVoice","chooseImage","previewImage","uploadImage","downloadImage","getNetworkType","openLocation","getLocation","hideOptionMenu","showOptionMenu","closeWindow","scanQRCode","chooseWXPay","openProductSpecificView","addCard","chooseCard","openCard"],"signature":"4a28bdb2f5cb8b2529439b16c02598668b2e00f7"})
	
	private Logger log = LoggerFactory.getLogger(IAuthService.class);	
	
	@Autowired
	IAccessTokenService accessTokenService;
	@Autowired
	IJSTicketService jsTicketService;
	
	
	public String getUserId(InstanceResource instanceResource,String code) throws Exception{
		log.info("IAuthService -- getUserId  code=" + code);
		String access_token = accessTokenService.getAccessToken(instanceResource).getAccessToken();
		
		String userId = super.fetchUserId(access_token, code);
		return userId;
	}
	
	
	public String getOauth2URL(InstanceResource instanceResource,String state) throws Exception{
		if (state == null) {
			state = "";
		}
		String url = instanceResource.getAuth().replace("CORPID", instanceResource.getCorpid()).replace("STATE", state);
		log.info("IAuthService -- getOauth2URL  url=" + url);
		return url;
	}
	
	public String getOpenidByUserid(InstanceResource instanceResource,String userid,String agentid) throws Exception{
		String access_token = accessTokenService.getAccessToken(instanceResource).getAccessToken();
		JSONObject param = new JSONObject();
		param.put("userid", userid);
		param.put("agentid", agentid);
		String openid = super.convertToOpenid(access_token, param.toJSONString());
		
		return openid;
	}
	
	public String getOpenidByUserid(InstanceResource instanceResource, String userid) throws Exception {
		return getOpenidByUserid(instanceResource, userid, null);
	}
	
	
	public String getUseridByOpenid(InstanceResource instanceResource,String openid) throws Exception{
		String access_token = accessTokenService.getAccessToken(instanceResource).getAccessToken();
		JSONObject param = new JSONObject();
		param.put("openid", openid);
		String userId = super.convertToUserid(access_token, param.toJSONString());
		
		return userId;
	}
	
	
	public String getSignJSInfo (InstanceResource instanceResource,JSONObject jsonParam) throws Exception{
		String jsticket = jsTicketService.getJSTicket(instanceResource).getjSTicket();
		String appid = instanceResource.getAppid();
		
		String url = jsonParam.getString("url");
		String noncestr = WechatUtil.getNonceStr();
		String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
		String jsSignature = WechatUtil.signJS(jsticket , noncestr , timestamp , url);
		
		log.info("IAuthService -- getSignJSInfo  jsticket=" + jsticket + " noncestr=" + noncestr + " timestamp=" +timestamp + " jsSignature=" + jsSignature);
		
		JSONObject infoObj = new JSONObject();
		infoObj.put("timestamp", timestamp);
		infoObj.put("nonceStr", noncestr);
		infoObj.put("signature", jsSignature);
		infoObj.put("appId", appid);
		
		log.info("IAuthService -- getSignJSInfo  infoObj=" + infoObj);
		
		return infoObj.toJSONString();
	}


}
